class Solution:
    def longestPalindrome(self, s):
        if len(s) == 0:
            return ""
        if len(s) == 1:
            return s
        if len(s) == s.count(s[0], 0, len(s)):
            return s

        print(len(s))
        print(s.count(s[0], 0, len(s)))
        l = len(s)
        max_str = s[0]

        # 这里使用了三重循环，时间复杂度较高。
        # 思想就是循环遍历所有可能的回文字符串。
        # 因为是找最长的，所以从逆序开始找
        for n in range(1, l).__reversed__():
            startPos = -1
            endPos = 0
            while startPos < l - n and endPos < l - 1:
                startPos += 1
                endPos = startPos + n
                i = startPos
                j = endPos
                while i < j:
                    if s[i] == s[j]:
                        i += 1
                        j -= 1
                    else:
                        break

                # 这里不用管长度取最长了，因为这个方法就是按长度依次递增来查找的，故长度大的一定在后面。
                if i >= j:
                    max_str = s[startPos:endPos + 1]

            if len(max_str) == l or len(max_str) > 1:
                break

        return max_str


s = Solution()

# 注意下面这个特例，非常长的字符串，给出了错误提示是Time Limit Exceeded
# result = s.longestPalindrome("abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababa")

# 下面这个例子就是从两边向中间一直循环比较。所以超时了。
# 下面这个串中间有个bc。他的最长回文子串是500个a
result = s.longestPalindrome(
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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